package org.darcy.youchat.activity.message;

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.netease.nimlib.sdk.InvocationFuture;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.team.constant.TeamFieldEnum;
import com.netease.nimlib.sdk.team.model.Team;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityTeamNameSetBinding;
import org.darcy.youchat.nimsdk.NimTeamSDK;
import org.darcy.youchat.utils.UIUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 群名片界面
 */
public class TeamNameSetActivity extends BaseActivity {

    private ActivityTeamNameSetBinding binding;
    public static final String TEAM_ID = "teamId";

    private String mTeamId;
    private Team mTeam;

    @Override
    public void init() {
        mTeamId = getIntent().getStringExtra(TEAM_ID);
        if (TextUtils.isEmpty(mTeamId)) {
            interrupt();
            return;
        }
        mTeam = NimTeamSDK.queryTeamBlock(mTeamId);
    }

    @Override
    public void initView() {
        binding = ActivityTeamNameSetBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setTitle("群名片");
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);

        binding.etName.setText(mTeam.getName());
        binding.etName.setSelection(mTeam.getName().length());
        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);
        binding.mToolbar.btnOk.setText("保存");

        binding.mToolbar.btnOk.setOnClickListener(v -> {
            final String teamName = binding.etName.getText().toString().trim();
            showWaitingDialog("修改群名片");
            Map<TeamFieldEnum, Serializable> fields = new HashMap<>(1);
            fields.put(TeamFieldEnum.Name, teamName);
            InvocationFuture<Void> invocationFuture = NimTeamSDK.updateTeamFields(mTeamId, fields);
            invocationFuture.setCallback(new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    hideWaitingDialog();
                    finish();
                }

                @Override
                public void onFailed(int code) {
                    UIUtils.showToast("修改群名片失败" + code);
                    hideWaitingDialog();
                }

                @Override
                public void onException(Throwable exception) {
                    exception.printStackTrace();
                    hideWaitingDialog();
                }
            });
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}
